package com.huangj.pro2_h2pop;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.huangj.adapter.HomeInfoAdapter;
import com.huangj.entity.HomeInfoEntity;
import com.huangj.huangjlibrary.base.BaseActivity;
import com.huangj.huangjlibrary.util.DownUtil;
import com.huangj.util.Constant;
import com.huangj.util.JsonUtil;
import com.huangj.wedigt.MyListview;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by HuangJ on 2016/10/4.14:20
 */
public class HomeInfoActivity extends BaseActivity implements DownUtil.OnDownListener {

    private int id;
    private String infoIv;
    @BindView(R.id.iv_homeinfo)
    ImageView iv_homeinfo;
    @BindView(R.id.tv_infoaddress)
    TextView tv_address;
    @BindView(R.id.tv_infoadeng)
    TextView tv_infoadeng;
    @BindView(R.id.ll_infoll)
    LinearLayout ll_info;
    @BindView(R.id.lv_homeinfo)
    MyListview listView_info;

    private String name;
    private String name_en;
    private List<HomeInfoEntity.DataBean.GoodsBean> goods;
    private HomeInfoAdapter homeInfoAdapter;
    private FragmentManager fragmentManager;

    @Override
    protected int getContentId() {
        return R.layout.homeinfo_layout;
    }

    @Override
    protected void init() {
        Intent intent = getIntent();
        id = intent.getIntExtra("id", -1);
        fragmentManager = getSupportFragmentManager();
        homeInfoAdapter = new HomeInfoAdapter(this,fragmentManager);
        listView_info.setAdapter(homeInfoAdapter);
    }
    @Override
    protected void loadDatas() {
        String url = String.format(Constant.URL_INFO,id);
        new DownUtil().setOnDownListener(this).downJSON(url);
    }
    /**
     * 解析JSON
     */
    @Override
    public Object paresJson(String json) {
        if(json!=null){
            return JsonUtil.getHomeInfoEntityJson(json);
        }
        return null;
    }
    @Override
    public void downSucc(Object object) {
        if (object != null){
            HomeInfoEntity homeInfoEntity = (HomeInfoEntity) object;
            List<HomeInfoEntity.DataBean.SectionsBean> sections = homeInfoEntity.getData().getSections();
//            Log.d("print", "downSucc: "+sections.get(0).getTitle());
            homeInfoAdapter.setDatas(sections);

            goods = homeInfoEntity.getData().getGoods();
            layoutparames(goods);

            name = homeInfoEntity.getData().getDestination().getName();
            name_en = homeInfoEntity.getData().getDestination().getName_en();
            tv_address.setText(name);
            tv_infoadeng.setText(name_en);
            infoIv = homeInfoEntity.getData().getDestination().getPhoto_url();
            Glide.with(this).load(infoIv)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .crossFade()
                    .placeholder(R.drawable.img_app_introduce)
                    .into(iv_homeinfo);
        }
    }

    public void layoutparames(final List<HomeInfoEntity.DataBean.GoodsBean> goods){
        ll_info.removeAllViews();
        //攻略机票等；
        if (goods != null){
            for (int i = 0; i < goods.size(); i++) {
                final int index = i;
                LinearLayout ll = new LinearLayout(this);
                ll.setGravity(Gravity.CENTER);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(180, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.topMargin = 10;
                layoutParams.weight = 1;
                ll.setOrientation(LinearLayout.VERTICAL);
                ll.setLayoutParams(layoutParams);
                TextView tv = new TextView(this);
                tv.setGravity(Gravity.CENTER);
                tv.setText(goods.get(i).getTitle());
                ImageView iv = new ImageView(this);
                LinearLayout.LayoutParams ivlp = new LinearLayout.LayoutParams(90,70);
                ivlp.bottomMargin = 10;
                iv.setLayoutParams(ivlp);
                Glide.with(this).load(goods.get(i).getPhoto_url()).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv);
                ll.addView(iv);
                //设置整体线性布局的监听；
                ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(HomeInfoActivity.this,HomeInfoWebActivity.class);
                        String url = (String) goods.get(index).getUrl();
                        if (url == null){
                            Toast.makeText(HomeInfoActivity.this, "该URL为空", Toast.LENGTH_SHORT).show();
                        }else{
                            intent.putExtra("url",url);
                            starActivityForAnimation(intent,R.anim.anim_show,R.anim.anim_dismiss);
                        }
                    }
                });
                ll.addView(tv);
                ll_info.addView(ll);
            }
        }
    }

    @OnClick(R.id.btn_infoback)
    public void btnClick(View view){
        this.finish();
    }


}
